﻿using System.IO;

using Vestras.StarCraft2.Luna.Parser;
using Vestras.StarCraft2.Luna.Parser.Ast;

namespace Vestras.StarCraft2.Luna.Validation {
    public sealed class IncludeTypeValidator : ITypeValidator {
        public System.Type[] Type {
            get {
                return new[] { typeof(Include) };
            }
        }

        public bool Validate(object value) {
            if (value != null && value is Include) {
                ErrorSink.UpdateChangeCollection();
                Include include = value as Include;
                if (!File.Exists(include.IncludedPath.GetProperIncludeString(include.FileName))) {
                    if (!ErrorSink.HasChanged()) {
                        ErrorSink.AddError(ErrorSink.Error.Create("Cannot find file '" + include.IncludedPath + "'", include));
                    }

                    return false;
                }

                return true;
            }

            return false;
        }
    }
}
